﻿# encoding: UTF-8
from strategy_platform.api import (sub_realmd,unsub_realmd,register_realmd_cb)
from strategy_platform.api import (sub_order, submit_order, cancel_order, register_order_cb)
from strategy_platform.api import (submit_order,query_order,cancel_order, query_position,query_commodity_info,query_credit_fund_info)
from strategy_platform.api import (second_timer, minute_timer, at_day_timer, get_trade_days)
from datetime import datetime, timedelta, date
import time, math, pandas

# no log.info allowed in this script

def get_prev_bizday(dt):
	dates = get_trade_days(20250901, end_date=dt, security_type="ashare", exchange="SH") 
	res   = 20250901
	for d in dates:
		int_d = int(d)
		if int_d < dt: res = int_d
	return res
	

def gen_targetpos_sk001(capital): #code, w1000, price, targetpos
	trade_date = int(date.today().strftime('%Y%m%d'))
	yesterday  = get_prev_bizday(trade_date)
	targetpos  = pandas.read_csv('D:/prod/sk001/targetpos/%d.csv'%yesterday) #code, w1000, price
	
	d = targetpos.copy()
	d['code'] = [a.replace('XSHG', 'SH').replace('XSHE', 'SZ') for a in d['code']]
	d['targetpos']  = capital * d.w1000 / d.price / d.w1000.sum()
	d['targetpos']  = [round(a/100)*100 for a in d.targetpos]
	d = d.fillna(0) #code, targetpos
	return d
	
	# eod    target   net    qty_open(net+eod)
	#   0    1000    1000     1000
	#1000       0   -1000     0     
	# 200    1000     800     1000
	# 1000   1000             1000
	# 1000   1200             1200
	
def get_eod(sname, dt): #code, eodpos
	filename = 'D:/prod/%s/eodpos/%d.csv'%(sname, dt)
	eod      = pandas.read_csv(filename) 
	eod['code'] = [a.replace('XSHG', 'SH').replace('XSHE', 'SZ') for a in eod['code']]
	
	eod.columns = ['code', 'eodpos']
	eod    = eod[eod.code != 'CASH']
	return eod[['code', 'eodpos']]


